package com.yaozheng.vocationaltraining.fragment.exam;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.activity.HomeActivity;
import com.yaozheng.vocationaltraining.adapter.LimitedChallengeAdapter;
import com.yaozheng.vocationaltraining.fragment.BaseFragment;
import com.yaozheng.vocationaltraining.iview.exam.ILimitedChallengeView;
import com.yaozheng.vocationaltraining.iview.limited.ITimedPaperView;
import com.yaozheng.vocationaltraining.service.exam.LimitedChallengeService;
import com.yaozheng.vocationaltraining.service.impl.exam.LimitedChallengeServiceImpl;
import com.yaozheng.vocationaltraining.service.impl.limited.TimedPaperServiceImpl;
import com.yaozheng.vocationaltraining.service.limited.TimedPaperService;
import com.yaozheng.vocationaltraining.utils.LogUtils;
import com.yaozheng.vocationaltraining.utils.TimeUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

/**
 * 限时挑战
 */
@EFragment((R.layout.activity_news_list))
public class LimitedChallengeFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, ILimitedChallengeView, ITimedPaperView {
    @ViewById
    ListView listView;
    @ViewById
    TextView headTitleText;
    /**
     * 下拉刷新
     */
    @ViewById
    SwipeRefreshLayout swipeRefreshLayout;

    LimitedChallengeAdapter limitedChallengeAdapter;

    @Bean(LimitedChallengeServiceImpl.class)
    LimitedChallengeService courseCatalogService;

    @Bean(TimedPaperServiceImpl.class)
    TimedPaperService timedPaperService;

    JSONArray dataList;
    JSONObject itemDate;

    public HomeActivity getHomeActivity() {
        return (HomeActivity) getActivity();
    }

    @AfterViews
    public void initView() {
        headTitleText.setText("限时挑战");
        courseCatalogService.init(this);
        timedPaperService.init(this);
        swipeRefreshLayout.setOnRefreshListener(this);
        onRefresh();
    }


    @Override
    public void onRefresh() {
        loadData();
    }

    public void loadData() {
        courseCatalogService.limitedChallengeList();
    }

    @Override
    public void cancelLoadDataProgressDialog() {
        swipeRefreshLayout.setRefreshing(false);
    }

    @UiThread
    @Override
    public void limitedChallengeListSuccess(JSONObject jsonObject) {
        cancelLoadDataProgressDialog();
        dataList = TypeUtils.getJsonArray(TypeUtils.getJsonObject(jsonObject, "data"), "exams");
        if (limitedChallengeAdapter == null) {
            limitedChallengeAdapter = new LimitedChallengeAdapter(getBaseActivity(), dataList);
            listView.setAdapter(limitedChallengeAdapter);
        } else {
            limitedChallengeAdapter.setDataList(dataList);
            limitedChallengeAdapter.notifyDataSetChanged();
        }
    }

    @UiThread
    @Override
    public void limitedChallengeListError(String error) {
        cancelLoadDataProgressDialog();
        alertMessage(error);
    }

    @ItemClick(R.id.listView)
    public void listViewItemClick(int position) {
        if (limitedChallengeAdapter != null) {
            itemDate = TypeUtils.getJsonObject(dataList, position);
            boolean start = TypeUtils.getJsonBoolean(itemDate, "start");
            long startTimeLong = TimeUtils.stringToTime(TypeUtils.getJsonString(itemDate, "startTime", ""));
            long endTimeLong = TimeUtils.stringToTime(TypeUtils.getJsonString(itemDate, "endTime", ""));
            long currTime = System.currentTimeMillis();
            if (start || (currTime >= startTimeLong && currTime <= endTimeLong)) {
                //参加考试
                showOperatingProgressDialog("取题中...");
                timedPaperService.createPapper(TypeUtils.getJsonInteger(itemDate, "id"));
            }
        }
    }


    @Override
    public void createPapperSuccess(JSONObject jsonObject) {
        cancelOperatingProgressDialog();
        JSONObject paperJsonObject = TypeUtils.getJsonObject(jsonObject, "data", "paper");
        int id = TypeUtils.getJsonInteger(paperJsonObject, "id");
        Intent intent = getBaseActivity().getIntent();
        intent.putExtra("examType", "5");
        intent.putExtra("paperId", id);
        intent.putExtra("questionJsonObject", paperJsonObject.toString());
        intent.putExtra("examTitle", TypeUtils.getJsonString(itemDate, "subject"));
        getHomeActivity().openOnlineExamFragment();
        itemDate = null;
    }

    @Override
    public void createPapperError(String error) {
        itemDate = null;
        cancelOperatingProgressDialog();
        alertMessage(error);
    }

    @Override
    public void onDestroyView() {
        dataList = null;
        limitedChallengeAdapter = null;
        itemDate = null;
        super.onDestroyView();
    }

}
